package com.ldk.api.handler;


import java.util.Hashtable;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ldk.api.ApiVersion;
import com.ldk.api.Consts;
import com.ldk.api.ErrorCode;
import com.ldk.api.ResponseObject;
import com.ldk.api.ResponseResult;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.base.framework.util.sms.AliDayu;
import com.ldk.base.framework.util.sms.SmsConfig;
import com.ldk.base.framework.util.sms.SmsFactory;
import com.ldk.base.framework.util.sms.SmsPlatform;
import com.ldk.framework.entity.PhoneValidcode;
import com.ldk.framework.service.IPhoneValidcodeService;

/**
 * 短信模块接口
 *
 * @author xiangwl
 * @date 2016-06-21
 * @time 11:16
 */
public class SmsRequestHandler extends BaseRequestHandler {

    @Autowired
    private IPhoneValidcodeService phoneValidCodeService;

    /**
     * 获取验证码
     *
     * @param parms   {phoneNo}
     * @param phoneNo 手机号
     * @return
     */
    @ApiVersion(value = {1})
    public ResponseObject getValidCode(Map<String, Object> parms) {
        ResponseObject response = ResponseResult.create("sms", "getValidCode");

        if (!checkRequireParms(parms, "phoneNo")) {
            return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
        }
        String phoneNo = String.valueOf(parms.get("phoneNo"));
        if (!phoneValidCodeService.checkExpired("", phoneNo)) {
            return ResponseResult.exception(response, ErrorCode.HAD_GET_VALID_CODE);
        }
        long sendTime = SystemUtil.getAllSeconds();
        PhoneValidcode query = new PhoneValidcode();
        query.setPhoneNo(phoneNo);
        PhoneValidcode model = phoneValidCodeService.selectByKey("", query);
        Boolean exist = model == null ? false : true;

        if (!exist) {
            model = new PhoneValidcode();
            model.setPhoneNo(phoneNo);
        }
        String validCode = SystemUtil.generateValidCode(6);

        System.out.println("validCode="+validCode);

        model.setValidCode(validCode);
        model.setSendTime(sendTime);
        model.setIsExpired(false);
        int row = 0;
        if (exist) {
            row = phoneValidCodeService.updateByKey("", model);
        } else {
            row = phoneValidCodeService.insert("", model);
        }
        Boolean sended = true;
        if (row > 0) {
            //调用短信接口
            String content = String.format("验证码：%s，请您在15分钟内填写。如非本人操作，请忽略本短信。", validCode);
            SmsConfig config = SmsConfig.newBuilder().setPlatform(SmsPlatform.Huyi)
                    .setAccessKey("C72876358")
                    .setAccessPwd("622b175678521b70d6fdda062268e426")
                    .setSignName(Consts.SystemName)
                    .setPhonNos(phoneNo)
                    .setContent(content)
                    .build();
            sended = SmsFactory.send(config);
        }
        if (sended) {
            Map<String, Object> result = new Hashtable<String, Object>();
            if (Consts.DEBUG) {
                result.put("validCode", validCode);
            }
            ResponseResult.success(response, result);
        } else {
            ResponseResult.exception(response, ErrorCode.GET_VALID_CODE_FAILED);
        }

        return response;

    }

    /**
     * 验证验证码是否过期
     *
     * @param parms     {phoneNo, validCode}
     * @paramphoneNo   手机号
     * @paramvalidCode 验证码
     * @return
     */
    @ApiVersion(value = {1})
    public ResponseObject checkExpired(Map<String, Object> parms) {
        ResponseObject response = ResponseResult.create("phoneNo", "checkExpired");

        if (!checkRequireParms(parms, "phoneNo", "validCode")) {
            return ResponseResult.exception(response, ErrorCode.REQUEST_PARMS_ERROR);
        }
        String phoneNo = String.valueOf(parms.get("phoneNo"));
        String validCode = String.valueOf(parms.get("validCode"));

        PhoneValidcode validCodeQuery = new PhoneValidcode();
        validCodeQuery.setPhoneNo(phoneNo);
        validCodeQuery.setValidCode(validCode);
        PhoneValidcode phoneValidCode = phoneValidCodeService.selectOne("", validCodeQuery);

        //ycs 190305
        /*
        //验证码错误
        if (phoneValidCode == null) {
            return ResponseResult.exception(response, ErrorCode.VALID_CODE_ERROR);
        }
        if (phoneValidCodeService.checkExpired("", phoneNo, validCode)) {
            return ResponseResult.exception(response, ErrorCode.VALIDE_CODE_TIME_OUT);
        }
        if (phoneValidCode.getIsExpired()) {
            return ResponseResult.exception(response, ErrorCode.VALIDE_CODE_TIME_OUT);
        }
        */

        return ResponseResult.success(response, "验证码有效");
    }
}
